#!/Users/hepeng/.pyenv/shims/python
# -*- coding: utf-8 -*-
# @Time    : 2020/8/31 3:38 下午
# @Author  : hepeng
# @Site    : 
# @File    : tools.py

from __future__ import unicode_literals
import os, shutil, datetime
from utils.LogFactory import LoggingHandler

LOG = LoggingHandler().getlogger()


def get_root_path():
    return os.path.dirname(os.path.dirname(__file__))

def get_play_book_path():
    return os.path.join(get_root_path(),'playbook')

def get_time_string():
    now = datetime.datetime.now()
    return now.strftime("%Y%m%d%H%M%S")


def cp_file(src_file, dest_dir):
    if os.path.exists(src_file):
        file_name = os.path.basename(src_file)
        dest_file_path = os.path.join(dest_dir, file_name)
        if os.path.exists(dest_file_path):
            back_up_file = file_name + '.bak' + get_time_string()
            back_up_all_path = os.path.join(dest_dir, back_up_file)
            shutil.move(dest_file_path, back_up_all_path)
            LOG.info('%s exist, rename: %s' % (dest_file_path, back_up_all_path))
        shutil.copy(src_file, dest_dir)
        LOG.info('%s copy to %s' % (src_file, dest_dir))
    else:
        # LOG.error("源文件不存在，请检查源文件。%s" % src_file)
        raise ValueError("源文件不存在，请检查源文件。%s" % src_file)

def rename_file(src_path,dest_path):
    os.rename(src_path,dest_path)
